package com.ztesoft.ossup.report.dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.domain.Report_;

import java.util.List;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.util.StringUtils;

/**
 * Created by luxiang on 16/9/21.
 */
public interface ReportRepository extends JpaRepository<Report, Long>, JpaSpecificationExecutor<Report> {

    class ReportSpecifications {

        public static Specification<Report> likeReportName(final String name) {
            return new Specification<Report>() {
				@Override
				public Predicate toPredicate(Root<Report> root, CriteriaQuery<?> criteriaQuery,
						CriteriaBuilder criteriaBuilder) {
						    if (!StringUtils.isEmpty(name))
						        return criteriaBuilder.like(root.get(Report_.name), "%" + name + "%");
						    return null;
						}
			};
        }
    }
    
    List<Report> findByCode(String code);
    
    Report findOneByCode(String code);
    
    @Query(value = "update report t set t.state = '10X',t.state_date=sysdate where t.id = ?1 ", nativeQuery = true)
    @Modifying
    public void updateReportStateById(long id);
    
    
    @Query(value = "update report t set t.state = '10A',t.state_date=sysdate,t.is_sum=?1,t.name=?2,t.sql=?3 where t.id = ?4 ", nativeQuery = true)
    @Modifying
    public void updateConfigReportById(String isSum,String name,String sql,long id);
}
